const fs = require('fs');
const path = require('path');

const gitHookPath = path.resolve('../../.git/hooks');

new Promise((resolve) => {
  fs.mkdir(gitHookPath, { recursive: true }, (err) => {
    if (err) {
      throw err;
    }
    resolve();
  });
}).then(() => {
  const shell = `#!/usr/bin/env bash\ngit submodule update\necho "子模块已更新！"`;
  const filePath = path.resolve(gitHookPath, 'post-checkout');
  fs.writeFile(filePath, shell, (error) => {
    if (error) {
      console.log(error);
      return;
    }
    fs.chmodSync(filePath, '755');
  });
});
